import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/login.vue'
import Person from '../views/person.vue'
import Search from '../views/search.vue'
import editPerson from '../views/editPerson.vue'
import Index from '../views/index.vue'
import Register from '../views/register.vue'
import articleDetail from '../views/articleDetail.vue'
import Myfollow from '../views/myFollow.vue'
import myStars from '../views/Mystars.vue'
import Comment from '../views/comment.vue'
import cateManage from '../views/cateManage.vue'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/index'
  },
  {
    path: '/index',
    name: 'index',
    component: Index
  },
  {
    path: '/comment/:id',
    name: 'Comment',
    component: Comment
  },
  {
    path: '/cateManage',
    name: 'cateManage',
    component: cateManage
  },
  {
    path: '/login',
    name: 'login',
    component: Login
  },
  {
    path: '/myStars',
    name: 'myStars',
    component: myStars
  },
  {
    path: '/register',
    name: 'register',
    component: Register
  },
  {
    path: '/myfollow',
    name: 'Myfollow',
    component: Myfollow
  },
 
  {
    path: '/person/:id',
    name: 'person',
    component: Person
  },
  {
    path: '/search',
    name: 'search',
    component: Search
  }, 
  {
    path: '/editPerson/:id',
    name: 'editPerson',
    component: editPerson
  }, 
  {
    path: '/articleDetail/:id',
    name: 'articleDetail',
    component: articleDetail
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // console.log(to.path, from);
  // console.log(to.path.indexOf('/person'));

  let token = localStorage.getItem('this_token');
  if (to.path.indexOf('/index') === 0 || to.path.indexOf('/login') === 0 ||to.path.indexOf('/search') === 0) {
    next()
  } else {
    if (token) {
      next()
    } else {
      next({ name: 'login' })
    }
  }
})
export default router
